package com.base.model.generator;

import org.apache.ibatis.io.Resources;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author FangShanShan
 * @Description 基于mybatis-generator 自动生成代码,用到java和xml文件相互配合
 * @Date  2020/6/11 11:55
 */
public class MyGeneratorByJavaXml {

    public void generator() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        /*指定 逆向工程配置文件*/

        String file = "mybatis/generatorConfig.xml";
        InputStream in = Resources.getResourceAsStream(file);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(in);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);
        in.close();
    }
    public static void main(String[] args) throws Exception {
        try {
            MyGeneratorByJavaXml generatorSqlmap = new MyGeneratorByJavaXml();
            generatorSqlmap.generator();
            System.out.println("生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}